HDU 5492 Find a path DP
Find a path
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5492Description
Frog fell into a maze. This maze is a rectangle containing N rows and M columns. Each grid in this maze contains a number, which is called the magic value. Frog now stays at grid (1, 1), and he wants to go to grid (N, M). For each step, he can go to either the grid right to his current location or the grid below his location. Formally, he can move from grid (x, y) to (x + 1, y) or (x, y +1), if the grid he wants to go exists.
Frog is a perfectionist, so he'd like to find the most beautiful path. He defines the beauty of a path in the following way. Let’s denote the magic values along a path from (1, 1) to (n, m) as A1,A2,…AN+M−1, and Aavg is the average value of all Ai. The beauty of the path is (N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=1(Ai−Aavg)2
In Frog's opinion, the smaller, the better. A path with smaller beauty value is more beautiful. He asks you to help him find the most beautiful path.
Input
The first line of input contains a number T indicating the number of test cases (T≤50).
Each test case starts with a line containing two integers N and M (1≤N,M≤30). Each of the next N lines contains M non-negative integers, indicating the magic values. The magic values are no greater than 30.
Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the minimum beauty value.
Sample Input
1
2 2
1 2
3 4
Sample Output
Case #1: 14
HINT
题意
给你一个矩阵,你可以往下走,也可以往右走
然后让你找到一条从(1,1)到(n,m)的路,使得(N+M−1)∑N+M−1i=1(Ai−Aavg)2的值最小
题解:
DP比较烦
dp,里面注意分母其实是一样的,可以化解,最后答案就是(n+m-1)*(sigma(a[i]^2)) - sigma^2,维护一下就好了
枚举平均值
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <stack> #include <map> #include <set> #include <queue> #include <iomanip> #include <string> #include <ctime> #include <list> #include <bitset> typedef unsigned char byte; #define pb push_back #define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0) #define local freopen("in.txt","r",stdin) #define pi acos(-1) using namespace std; const int maxn = 30 + 10; int dp[maxn][maxn][2000]; int g[maxn][maxn] , n , m , px ,dir[2][2] = {0,1,1,0},tans; int cal(int x,int y) { return px*x - y*y; } bool inmap(int x,int y) { return x <= n && x >= 1 && y <= m && y >= 1; } void initiation() { tans = 1 << 29; scanf("%d%d",&n,&m); for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j) scanf("%d",&g[i][j]); px = n + m - 1; memset(dp,-1,sizeof(dp)); dp[1][1][g[1][1]] = g[1][1]*g[1][1]; } void updata(int &x ,int v) { if(x==-1) x=v; else x = min(x,v); } int solve() { for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j) { for(int z = 0 ; z <= 1900 ; ++ z) { if(dp[i][j][z] == -1) continue; for(int k = 0 ; k < 2 ; ++ k) { int newx = i + dir[k][0]; int newy = j + dir[k][1]; if(!inmap(newx,newy)) continue; int newval = dp[i][j][z] + g[newx][newy]*g[newx][newy]; updata(dp[newx][newy][z+g[newx][newy]],newval); } } } int ans = 1 << 30; for(int i = 0 ; i <= 1900 ; ++ i) if(dp[n][m][i] != -1) ans = min(ans , px*dp[n][m][i] - i*i); return ans; } void dfs(int x,int y,int s1,int s2) { if(x==n&&y==m) { tans =min(tans,s1*px-s2*s2); } else { for(int k = 0 ; k < 2 ; ++ k) { int newx = x + dir[k][0]; int newy = y + dir[k][1]; if(!inmap(newx,newy)) continue; dfs(newx,newy,s1+g[newx][newy]*g[newx][newy],s2+g[newx][newy]); } } } int main(int argc,char *argv[]) { int Case,cas=1; scanf("%d",&Case); while(Case--) { initiation(); printf("Case #%d: %d\n",cas++,solve()); // dfs(1,1,g[1][1]*g[1][1],g[1][1]); } return 0; }